import BaseUtil from '@/BaseUtil';
import MathUtil from '@/MathUtil';
import SelectUtil from '@/SelectUtil';
import MovementCommon from 'code/Core/Common/MovementCommon';
import AbilityBase from 'code/Scripts/Abilities/base/AbilityBase';
/**
 * 冲撞
 **/
export default class BossSpell_ChongZhuang extends AbilityBase {
    static instance: BossSpell_ChongZhuang;
    constructor(id: string = 'Mzw6') {
        super(id);
        BossSpell_ChongZhuang.instance = this;
        this.init();
        se.onUnitSpellEffect(this.onSpellEffect.bind(this), this.id);
    }
    init() {}
    cast<T extends Object = any>(tarX: number, tarY: number, source: unit, target: unit, data?: T): void {
        BossAbilityManagerInstance.text('冲撞', source);
        target = SelectUtil.getRandomEnemyInRange(source, 1000);
        let x = GetUnitX(target);
        let y = GetUnitY(target);
        let mark = AddSpecialEffect(BossAbilityManagerInstance.SpellMark, x, y);
        BaseUtil.runLater(1, () => {
            this.effect(source, mark);
            source = null;
            mark = null;
        });
    }
    effect(source: unit, mark: effect) {
        let dmagePool = [];
        let ang = MathUtil.angleBetweenCoords(GetUnitX(source), GetUnitY(source), EXGetEffectX(mark), EXGetEffectY(mark));
        MovementCommon.hitFly({
            target: source,
            caster: source,
            angle: ang,
            speed: 900,
            distance: 800,
            maxHeight: 0,
            onInterval: this.onMoving.bind(this),
            onEnd: this.onEnd.bind(this),
            other: {
                dmagePool,
                mark,
            },
        });
    }
    onEnd(p: HitFlyConfig<{ dmagePool: unit[]; mark: effect }>) {
        DestroyEffect(p.other.mark);
        p.other.dmagePool = null;
    }
    onMoving(p: HitFlyConfig<{ dmagePool: unit[]; mark: effect }>) {
        // if (p.curLoop % 5 == 1) {
        DestroyEffect(AddSpecialEffect('Abilities\\Weapons\\TreantMissile\\TreantMissile.mdl', p.curX, p.curY));
        // }
        let es = SelectUtil.getEnemyUnitsInRange(p.caster, 125);
        for (let e of es) {
            if (p.other.dmagePool.includes(e)) continue;
            p.other.dmagePool.push(e);
            DamageSystemInstance.applyPhysicalDamage(e, p.caster, 1200);
            StunSystemIntance.stun(e, 2, p.caster);
        }
        return true;
    }
    onSpellEffect() {
        this.cast(GetSpellTargetX(), GetSpellTargetY(), GetTriggerUnit(), GetSpellTargetUnit());
    }
}
